import api from "@/api";
export default{
    namespaced:true,
    state:{
        token:localStorage.getItem('token')||null,
        user:JSON.parse(localStorage.getItem('userInfo')||'{}')
    },
    mutations:{
        //设置Token
        SET_TOKEN(state,payload){
            state.token=payload;
            //缓存到本地
            localStorage.setItem('token',payload)
        },
        REMOVE_TOKEN(state,payload){
            state.token=null;
            localStorage.removeItem('token')
        },
        SET_USER_INFO(state,payload){
            state.user=payload
            //缓存到本地
            localStorage.setItem('userInfo',JSON.stringify(payload))
        },
        REMOVE_USER_INFO(state){
            state.user={}
            localStorage.removeItem('userInfo')
        }
    },
    actions:{
        async loginAsync(cxt,payload){
            let {code,message,token}=await api.users.loginApi(payload);
            if(code){
                cxt.commit('SET_TOKEN',token)
                return message
            }
        },
        async getUserInfoAsync({commit}){
            const result=await api.users.getUserInfoApi()
            commit('SET_USER_INFO',result.data)
        }
    }
}